package com.example.service;

import com.example.dto.MbtiAnswer;
import com.example.entity.MbtiQuestion;
import com.example.entity.MbtiTestResult;
import com.example.mapper.MbtiQuestionMapper;
import com.example.mapper.MbtiTestResultMapper;
import com.example.vo.StudentMBTIResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MbtiTestService {
    @Resource
    private MbtiQuestionMapper mbtiQuestionMapper;
    @Resource
    private MbtiTestResultMapper mbtiTestResultMapper;
    public MbtiTestResult submitTest(Integer userId, List<MbtiAnswer> answers) {
        // 获取所有问题
        List<MbtiQuestion> allQuestions = mbtiQuestionMapper.findAll();

        // 将问题按ID映射
        Map<Integer, MbtiQuestion> questionMap = allQuestions.stream()
                .collect(Collectors.toMap(MbtiQuestion::getQuestionId, q -> q));

        // 初始化四个维度的分数
        int eScore = 0, iScore = 0;
        int sScore = 0, nScore = 0;
        int tScore = 0, fScore = 0;
        int jScore = 0, pScore = 0;

        // 计算每个维度的分数
        for (MbtiAnswer answer : answers) {
            MbtiQuestion question = questionMap.get(answer.getQuestionId());
            if (question == null) {
                continue;
            }

            String dimension = question.getDimension(); // 如 "EI", "SN", "TF", "JP"
            String choice = answer.getChoice();

            switch (dimension) {
                case "EI":
                    if ("a".equals(choice)) eScore++;
                    else if ("b".equals(choice)) iScore++;
                    break;
                case "SN":
                    if ("a".equals(choice)) sScore++;
                    else if ("b".equals(choice)) nScore++;
                    break;
                case "TF":
                    if ("a".equals(choice)) tScore++;
                    else if ("b".equals(choice)) fScore++;
                    break;
                case "JP":
                    if ("a".equals(choice)) jScore++;
                    else if ("b".equals(choice)) pScore++;
                    break;
            }
        }

        // 确定MBTI类型
        String mbtiType = determineMbtiType(eScore, iScore, sScore, nScore, tScore, fScore, jScore, pScore);

        // 创建测试结果对象
        MbtiTestResult result = new MbtiTestResult();
        result.setUserId(userId);
        result.setMbtiType(mbtiType);
        result.setCreateTime(new Date());

        // 保存结果到数据库
        mbtiTestResultMapper.insert(result);

        // 获取完整MBTI类型信息
        return result;
    }

    private String determineMbtiType(int e, int i, int s, int n, int t, int f, int j, int p) {
        StringBuilder mbtiType = new StringBuilder();
        mbtiType.append(e > i ? "E" : "I");
        mbtiType.append(s > n ? "S" : "N");
        mbtiType.append(t > f ? "T" : "F");
        mbtiType.append(j > p ? "J" : "P");
        return mbtiType.toString();
    }


    public List<MbtiTestResult> historyTest(Integer userId) {
        return mbtiTestResultMapper.historyTest(userId);
    }

    public List<StudentMBTIResult> getClassStudentMBTIResults(Integer classId) {
        return mbtiTestResultMapper.findLatestMBTIResultsByClassId(classId);
    }
}
